package com.globalwave.website.bulletin
{
    import com.globalwave.common.event.RecordEvent;
    import com.globalwave.util.HttpServiceUtil;
    
    import flash.display.DisplayObject;
    
    import mx.collections.ArrayCollection;
    import mx.controls.Alert;
    import mx.core.Application;
    import mx.events.CloseEvent;
    import mx.managers.PopUpManager;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.http.HTTPService;
  
    public class F {
        public function F(displayObject : DisplayObject) {
            this.displayObject = displayObject;
        }     
        
        private var deleteObjects : Array ;
        private var displayObject : DisplayObject ;
            
        public function openCreateWindow():void {
          
            var app : DisplayObject = DisplayObject(Application.application) ;
            var editWindow : BulletinEditWindow = 
                    BulletinEditWindow(PopUpManager.createPopUp(app, BulletinEditWindow, true));
                    
            editWindow.bizDispatcher = displayObject ;
            //editWindow.addEventListener(RecordEvent.ADD_SUCCESS,dispatchRecordEvent);
            //editWindow.addEventListener(CloseEvent.CLOSE ,closeWindowCompleted);
        }
      
        public function openUpdateWindow(updateObject : Object):void {
            if (updateObject == null) {
                Alert.show("请选择要编辑的用户！！") ;
                return ;
            }
          
            var app : DisplayObject = DisplayObject(Application.application) ;
            var editWindow : BulletinEditWindow = 
                    BulletinEditWindow(PopUpManager.createPopUp(app, BulletinEditWindow, true));
          
            editWindow.updateObject = updateObject ;
            editWindow.bizDispatcher = displayObject ;
          
           // editWindow.addEventListener(RecordEvent.UPDATE_SUCCESS,dispatchRecordEvent);
          //dispatchEvent(new RecordEvent("recordUpdateSuccess")) ;
        }
          

        public function deleteRecord(deleteObjects: Array):void {
            if (deleteObjects == null) {
                Alert.show( "请选择要删除的记录！" );
                return ;
            }
            
            this.deleteObjects = deleteObjects ;
               
            var msg : String ;
            if (deleteObjects == null || deleteObjects.length == 1) {
                msg = "是否删除[" + deleteObjects[0].topic_title + "]？" ;
            } else {
                msg = "是否删除所有记录？" ;
            }
            Alert.show( msg , "删除确认", Alert.YES|Alert.NO, null, deleteRecordConfirm);                
        }
        
        private function deleteRecordConfirm(event:CloseEvent):void {
            if (event.detail==Alert.YES) {
                var params :Object = new Object();
                                      
                params["012345678"] = "012345678" ;

                var ids : ArrayCollection = new ArrayCollection() ;
                for (var i : int = 0 ; i < deleteObjects.length ; i ++) {
                    ids.addItem(deleteObjects[i]['bulletin_id']) ;
                }
                params["bulletin_ids"] = ids.toArray() ;
                          
                var httpService : HTTPService = null;
                httpService = HttpServiceUtil.getService("/website/bulletin_delete");
                httpService.addEventListener(ResultEvent.RESULT,deleteRecordCompleted) ;
    
                httpService.send(params);             
            }            
        }        
          
        private function deleteRecordCompleted(event:ResultEvent) : void {
            var result : XML = new XML(event.result) ;
            if(result.elements("code") != "0") {
                return ;
            }
            
            displayObject.dispatchEvent(new RecordEvent(RecordEvent.DELETE_SUCCESS)) ;
        
        }
        
        /*
        public function dispatchRecordEvent(event:RecordEvent) : void {
            displayObject.dispatchEvent(event.copy()) ;
        }
        */
    }
}